<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,initial-scale=1.0,width=device-width" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>支付保障金</title>
    <link rel="stylesheet" type="text/css" href="../../../css/aui/aui.css" />
    <link rel="stylesheet" type="text/css" href="../../../css/html/issued/post_your_want_frm.css" />
    <style>
        .row_flex {
            display: flex;
            justify-content: space-between;
        }

        .list_item {
            padding: 0.5rem 0.75rem;
            border-bottom: 1px solid #eee;
            display: flex;
            justify-content: space-between;
        }

        .pay_icon {
            display: flex;
            align-items: center;
        }

        .pay_icon img {
            width: 1rem;
            height: 1rem;
            margin-right: 0.5rem;
        }

        [v-clock] {
            display: none;
        }
    </style>
</head>

<body>
    <div id="app" v-clock>
        <div class="aui-content aui-margin-b-15">
            <ul class="aui-list aui-form-list">
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">保障金</div>
                        <div class="aui-list-item-input" style="display:flex;align-items:center;">
                            <input type="text" v-model="authPrice.security_price">元
                        </div>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">保险金</div>
                        <div class="aui-list-item-input">
                            <div class="aui-list-item-input" style="display:flex;justify-content: flex-end;padding-right:0;font-size:0.6rem;">
                                <label><input class="aui-radio" @click="baozhangjin()" type="radio" v-model="info.is_insure" value="0" name="demo1"> 不缴纳￥0.00</label>
                                <label><input class="aui-radio" @click="baozhangjin()" type="radio" v-model="info.is_insure" value="1" name="demo1"> 缴纳￥{{authPrice.insurance_price}}</label>
                            </div>
                        </div>
                    </div>
                </li>
                <div class="row_flex" style="padding:0.5rem 0.75rem;">
                    <div>总计金额</div>
                    <div style="color:#F52E2E;">￥{{info.security_price + info.insurance_price}}</div>
                </div>
            </ul>

            <div style="border-top:0.5rem solid #eee;border-bottom:0.5rem solid #eee;">
                <div class="list_item" v-show="wxStatus">
                    <div class="pay_icon"><img src="../../../image/icon/weixin.png" alt="">微信支付</div>
                    <input class="aui-radio" type="radio" name="pay_type" v-model="info.pay_types" value="2">
                </div>
                <div class="list_item">
                    <div class="pay_icon"><img src="../../../image/icon/zhifubao.png" alt="">支付宝支付</div>
                    <input class="aui-radio" type="radio" name="pay_type" v-model="info.pay_types" value="3">
                </div>
                <div class="list_item">
                    <div class="pay_icon"><img src="../../../image/icon/yue.png" alt="">余额支付</div>
                    <div>￥{{userInfo.wallet_balance}}<input class="aui-radio" type="radio" name="pay_type" v-model="info.pay_types" value="1"></div>
                </div>
            </div>
        </div>
    </div>
</body>
<script src="../../../script/api.js" charset="utf-8"></script>
<script src="../../../script/aui/aui-tab.js" charset="utf-8"></script>
<script src="../../../script/common/jquery-3.3.1.min.js" charset="utf-8"></script>
<script src="../../../script/common/common.js" charset="utf-8"></script>
<script src="../../../script/common/config.js" charset="utf-8"></script>
<script src="../../../script/common/vue.min.js" charset="utf-8"></script>
<script>
    apiready = function() {
        api.parseTapmode();
        vm.init();
    }

    var vm = new Vue({
        el: "#app",
        data: {
            wxStatus: true,
            userInfo: {},
            authPrice: {
                insurance_price: '',
                security_price: '',
            },
            info: {
                user_id: '',
                insurance_price: '',
                security_price: '',
                pay_types: 3,
                is_insure: 1,
            }
        },
        methods: {
            init() {
                this.userInfo = get_local('userInfo');
                this.info.user_id = this.userInfo.user_id;
                this.getAuthPrice();
            },
            getAuthPrice() {
                var that = this;
                _ajax(Config.api.AuthUserGetAuthPrice, {}, function(ret) {
                    if (ret.code === 200) {
                        that.authPrice = ret.data;
                        that.info.insurance_price = ret.data.insurance_price;
                        that.info.security_price = ret.data.security_price;
                    }
                });
            },
            pay() {
                var that = this;
                var countPrice = this.info.insurance_price + this.info.security_price;
                if (this.info.pay_types == 1 && this.withinErrorMargin(this.userInfo.wallet_balance, countPrice)) {
                    $toast("余额不足");
                    return;
                } else {
                    _ajax(Config.api.AuthUserGoPay, this.info, function(ret) {
                        if (ret.code === 200) {
                            if (that.info.pay_types == 1) {
                                that.apySuccess();
                            } else if (that.info.pay_types == 2) {
                                that.wxPay(ret.data);
                            } else if (that.info.pay_types == 3) {
                                that.aliPay(ret.data.orderInfo);
                            }
                        } else {
                            $toast(ret.mags + ret.code);
                        }
                    });
                }
            },
            // 2个数比较
            withinErrorMargin(left, right) {
                return Math.abs(left - right) < Number.EPSILON * Math.pow(2, 2);
            },
            apySuccess() {
                $toast("支付成功");

                api.execScript({
                    name: 'main',
                    frameName: 'my',
                    script: 'vm.gerUserInfo()'
                });

                setTimeout(function() {
                    close_w();
                }, 1000);
            },
            wxPay(data) {
                var that = this;
                var wxPay = api.require('wxPay');
                wxPay.payOrder(data, function(ret, err) {
                    if (ret.status) {
                        that.apySuccess();
                    } else {
                        // var errInfo = {
                        //     '1': 'apiKey值非法',
                        //     '-1': '未知错误，可能的原因：签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等',
                        //     '-2': '用户取消，发生场景：用户不支付了，点击取消，返回APP',
                        // }
                        // $toast(errInfo[err.code] + ' 错误码：' + err.code);
                        $toast('支付失败，请尝试其他方式支付' + err.code);
                    }
                });
            },
            aliPay(orderInfo) {
                var that = this;
                var aliPay = api.require('aliPay');
                aliPay.payOrder({
                    orderInfo: orderInfo
                }, function(ret, err) {
                    if (ret.code == 9000) {
                        that.apySuccess();
                    } else {
                        // var errInfo = {
                        //     '8000': '正在处理中，支付结果未知（有可能已经支付成功），请查询商户订单列表中订单的支付状态',
                        //     '4000': '订单支付失败',
                        //     '5000': '重复请求',
                        //     '6001': '用户中途取消支付操作',
                        //     '6002': '网络连接出错',
                        //     '6004': '支付结果未知（有可能已经支付成功），请查询商户订单列表中订单的支付状态',
                        // }
                        // $toast(errInfo[ret.code] + ' 错误码：' + ret.code);
                        $toast('支付失败，请尝试其他方式支付' + ret.code);
                    }
                });
            },
            baozhangjin() {
                this.info.insurance_price = this.info.is_insure == 1 ? this.authPrice.insurance_price : 0;
            }
        }
    });
</script>

</html>
